package com.baidu.mpks.statistic.meta;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class QueryCount implements Serializable {
    private String query;
    private long count;
    private float ratio;

    @JsonIgnore
    private transient List<UserCount> userCounts;

    public QueryCount() {
    }

    public QueryCount(String query, long count) {
        this.query = query;
        this.count = count;
    }

    public QueryCount(String query, long count, List<UserCount> userCounts) {
        this.query = query;
        this.count = count;
        this.userCounts = userCounts;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public float getRatio() {
        return ratio;
    }

    public void setRatio(float ratio) {
        this.ratio = ratio;
    }

    public void removeUserCount(String userId) {
        if (count <= 0 || userCounts == null) {
            return;
        }
        for (UserCount userCount : userCounts) {
            if (userCount.getId().equals(userId)) {
                count = Math.max(count - userCount.getCount(), 0);
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        QueryCount query1 = (QueryCount) o;
        return query.equals(query1.query);
    }

    @Override
    public int hashCode() {
        return Objects.hash(query);
    }

    @Override
    public String toString() {
        return "{" +
                "\"query\":\"" + query + "\"," +
                "\"count:\"" + count + "\"," +
                "\"ratio:\"" + ratio + "\"" +
                '}';
    }
}
